# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, HTTPException, Security, Request

from core.response import success_page, ResponsePage, ResponseModel, success
from core.dependencies import page_filter

from app.models.request.role import Permision, PermisionCreate, PermisionUpdate
from app.repository.permision import PermisionRepository

from app.service.user import check_login_user

router = APIRouter(prefix="/permision")

@router.get("/", response_model=ResponsePage[Permision], summary="权限列表查询", dependencies=[Security(check_login_user, scopes=["permision_access"])])
async def permision_pages(pageNo: int=1, pageSize: int=10
        , page_filter = Depends(page_filter)
        , repo: PermisionRepository = Depends(PermisionRepository)):
    data, total_page, total_rows = await repo.get_by_page(pageNo, pageSize, page_filter)
    return success_page(page_no=pageNo, page_size=pageSize, total_page = total_page, total_rows=total_rows, data=data)


@router.get("/{id}", response_model=ResponseModel[Permision], summary="权限信息查询", dependencies=[Security(check_login_user, scopes=["permision_access"])])
async def permision_by_id(id: int, repo: PermisionRepository = Depends(PermisionRepository)):
    db_data = await repo.get_model_by_id(id)
    if db_data is None:
        raise HTTPException(status_code=404, detail="Permision not found")
    
    return success(data=Permision.from_orm(db_data))


@router.post("/", response_model=ResponseModel[Permision], summary="权限创建", dependencies=[Security(check_login_user, scopes=["permision_create"])])
async def do_create_permision(permision: PermisionCreate, request: Request, repo: PermisionRepository = Depends(PermisionRepository)):
    check_data = (await repo.get_filter(filter={"name": permision.name})).first()
    if check_data is not None:
        raise HTTPException(status_code=400, detail="Permision already exists")
    permision = await repo.save(permision, create_user=request.state.user_id)
    return success(data=Permision.from_orm(permision))

@router.delete("/{id}", response_model=ResponseModel[Permision], summary="权限删除", dependencies=[Security(check_login_user, scopes=["permision_delete"])])
async def do_delete_permision(id: int, request: Request, repo: PermisionRepository = Depends(PermisionRepository)):
    check_data = await repo.get_by_id(id)
    if check_data is None:
        raise HTTPException(status_code=400, detail="Permision not found")
    
    data = await repo.delete(id, delete_user=request.state.user_id)
    return success(data=Permision.from_orm(data))

@router.put("/{id}", response_model=ResponseModel[Permision], summary="权限信息修改", dependencies=[Security(check_login_user, scopes=["permision_update"])])
async def do_update_permision(id: int, data: PermisionUpdate, request: Request, repo: PermisionRepository = Depends(PermisionRepository)):
    check_data = await repo.get_model_by_id(id)
    if check_data is None:
        raise HTTPException(status_code=400, detail="Permision not found")
    
    data = await repo.update(id, data, update_user=request.state.user_id)
    return success(data=Permision.from_orm(data))
